package com.tarena.lbs.coupon.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.dao.repository.CouponCodeRepository;
import com.tarena.lbs.coupon.dao.repository.CouponRepository;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.po.CouponCodePO;
import com.tarena.lbs.coupon.pojo.po.CouponPO;
import com.tarena.lbs.coupon.pojo.query.CouponQuery;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import com.tarena.lbs.coupon.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponRepository couponRepository;

    @Override
    public PageResult<CouponVO> pageList(CouponQuery query) {
        log.debug("查询优惠券列表,service层{}", query);
        PageResult<CouponVO> pageResult = new PageResult<>();
        pageResult.setPageNo(query.getPageNo());
        pageResult.setPageSize(query.getPageSize());
        pageResult.setTotal(100L);

        List<CouponPO> pos = couponRepository.couponList(query);
        List<CouponVO> vos = null;
        if (pos != null && pos.size() > 0) {
            vos = pos.stream().map(po -> {
                CouponVO vo = new CouponVO();
                BeanUtils.copyProperties(po, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        pageResult.setObjects(vos);
        return pageResult;
    }
   @Autowired
   private CouponCodeRepository couponCodeRepository;
    private PageInfo<String> getPageCouponCode(CouponQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<String> codePos = couponCodeRepository.getList();
        return new PageInfo<>(codePos);
    }

    private PageInfo<CouponPO> getPageCoupon(CouponQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<CouponPO> pos=couponRepository.couponList(query);
        return new PageInfo<>(pos);
    }
    @Override
    public void addCoupon(CouponParam couponParam) {
        log.debug("新增优惠券,service层{}", couponParam);
        CouponPO couponPO = new CouponPO();
        BeanUtils.copyProperties(couponParam, couponPO);
        couponPO.setBusinessId(1);
        couponPO.setStatus(1);
        couponPO.setCreateAt(new Date());
        couponPO.setUpdateAt(new Date());
        couponPO.setUsageNum(0);
        couponPO.setStartDate(new Date());
        couponPO.setEndDate(new Date());
        couponPO.setEnableStatus(1);
        couponPO.setUsageLimit(1);
        couponRepository.addCoupon(couponPO);
    }

    @Override
    public CouponVO detail(Integer id) {
        log.debug("查询优惠券详情,service层");
        CouponPO couponPO = couponRepository.detail(id);
        if (couponPO == null) {
            // 可以选择抛出自定义异常或者直接返回 null / 空对象
            throw new IllegalArgumentException("未找到对应的优惠券信息，ID：" + id);
        }
        CouponVO couponVO = new CouponVO();
        BeanUtils.copyProperties(couponPO,couponVO );
        return couponVO;
    }


}
